# test_client.py
import requests
import json
import time
import os

def llm_extract_pdf_info(pdf_file):
    """测试API服务"""
    base_url = "http://localhost:8000"

    # # 1. 健康检查
    # print("🔍 健康检查...")
    # try:
    #     health_response = requests.get(f"{base_url}/health")
    #     print(f"健康状态: {health_response.json()}")
    # except Exception as e:
    #     print(f"❌ 服务不可达: {e}")
    #     return
    #
    # # 2. 根路径
    # print("\n🏠 根路径检查...")
    # root_response = requests.get(f"{base_url}/")
    # print(f"服务状态: {root_response.json()}")

    # 3. 文件上传测试
    print("\n📤 文件上传测试...")
    # pdf_file = r"D:\存档\存档-已导入\CBHU9451440-完结\BL-OOLU2161707750.pdf"  # 替换为您的测试文件路径
    # pdf_file = r"D:\存档\存档-待核对\华宇-10.23\FSCU8227108-KO-10.24-已做\FSCU8227108-AN.pdf"  # 替换为您的测试文件路径
    # pdf_file = r"D:\存档\存档-已导入\CSLU6010518-完结\BL-COSU6427897751.pdf"  # 替换为您的测试文件路径
    # pdf_file = r"D:\存档\存档-待核对\华宇-10.23\BSIU8448283-AVERRA-10.24-已做\BSIU8448283-AN.PDF"  # 替换为您的测试文件路径

    if os.path.exists(pdf_file):
        try:
            with open(pdf_file, 'rb') as f:
                files = {'file': (pdf_file, f, 'application/pdf')}
                data = {'file_type': 'pdf'}

                start_time = time.time()
                response = requests.post(f"{base_url}/extract", files=files, data=data)
                processing_time = time.time() - start_time

                if response.status_code == 200:
                    result = response.json()
                    print(f"✅ 提取成功! 处理时间: {processing_time:.2f}秒")
                    print("提取结果:")
                    print(json.dumps(result, indent=2, ensure_ascii=False))
                    return result
                else:
                    print(f"❌ 提取失败: {response.status_code}")
                    print(response.json())
                    return response.json()
        except Exception as e:
            print(f"❌ 文件上传测试失败: {e}")
    else:
        print(f"⚠️ 测试文件不存在: {pdf_file}")

    # 4. 批量测试（可选）
    print("\n🔄 批量测试准备...")
    return None
    # 这里可以添加批量测试逻辑


if __name__ == "__main__":
    # test_file = r"D:\存档\存档-已导入\CBHU9451440-完结\BL-OOLU2161707750.pdf"  # 替换为您的测试文件路径
    # test_file = r"D:\存档\存档-待核对\华宇-10.23\FSCU8227108-KO-10.24-已做\FSCU8227108-AN.pdf"  # 替换为您的测试文件路径
    test_file = r"D:\存档\存档-已导入\CSLU6010518-完结\BL-COSU6427897751.pdf"  # 替换为您的测试文件路径
    # test_file = r"D:\存档\存档-待核对\华宇-10.23\BSIU8448283-AVERRA-10.24-已做\BSIU8448283-AN.PDF"  # 替换为您的测试文件路径
    llm_extract_pdf_info(test_file)